package udpsok

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
	"net"
	"UDPloadbalance/rpc/service"
)

type server struct {}

func (s *server) OpenDoor(ctx context.Context, in *service.OpenDoorReq)(*service.OpenDoorResult,error){
	err:=HandleOpenDoor(in.Sn,int8(in.Num))
	if err!=nil{
		return &service.OpenDoorResult{Message:err.Error(),Code:0},nil
	}else{
		return &service.OpenDoorResult{Message:"success",Code:1},nil
	}

}


func StartGrpcService(Port string) {
	lis, err := net.Listen("tcp", ":"+Port)
	if err != nil {
		fmt.Printf("监听端口失败: %s", err)
		return
	}
	// 创建gRPC服务器
	s := grpc.NewServer()
	// 注册服务
	service.RegisterRouterServer(s, &server{})

	reflection.Register(s)
	err = s.Serve(lis)
	if err != nil {
		fmt.Printf("开启服务失败: %s", err)
		return
	}
}